package com.charging.controller.admin;

import com.charging.entity.Announcement;
import com.charging.entity.ChargingPile;
import com.charging.service.AnnouncementService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin/announcement")
public class AnnounceController {
    @Resource
    private AnnouncementService announcementService;

    // 列表页
    @GetMapping("/list")
    public String list(Model model) {
        List<Announcement> announcementList = announcementService.getAllValidAnnouncements();
        model.addAttribute("announcementList", announcementList);
        return "admin/announcement/list";
    }

    // 新增页
    @GetMapping("/add")
    public String showAddForm(Model model) {
        model.addAttribute("announcement", new Announcement());
        return "admin/announcement/add";
    }

    @PostMapping("/add")
    public String add(Announcement announcement) {
        announcementService.add(announcement);
        return "admin/announcement/list";
    }

    @GetMapping("/detail/{id}")
    public String announcementDetail(@PathVariable Long id, Model model) {
        model.addAttribute("announcement", announcementService.getAnnouncementById(id));
        return "admin/announcement/detail";
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        announcementService.delete(id);
        return "admin/announcement/list";
    }
}
